﻿package net.pezzato.graphics {
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.display.DisplayObject;
	/**
	 * ...
	 * @author Alessandro Pezzato
	 */
	public class FullStagePhoto extends Sprite {
		private var url_:String;
		private var img_:Loader;
		private var maxSize_:Number;
		private var pb_:ProgressBar;
		public function FullStagePhoto(url:String, p:DisplayObject) {
			p.stage.addChild(this);
			adaptSizeToStage(p.stage);
			load(url);
		}
		public function get progressBar():ProgressBar {
			return pb_;
		}
		private function load(url:String):void {
			img_ = new Loader();
			img_.contentLoaderInfo.addEventListener(Event.COMPLETE, onPhotoLoaded);
			var request:URLRequest = new URLRequest(url);
			addProgressBar();
			img_.load(request);
			alpha = 0;
		}
		private function onPhotoLoaded(event:Event):void {
			if (pb_.parent != null) {
				pb_.parent.removeChild(pb_);
			}
			addChild(img_);
			rescale();
			center();
			addEventListener(Event.ENTER_FRAME, onEnterFrameOpen);
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(e:MouseEvent):void {
			addEventListener(Event.ENTER_FRAME, onEnterFrameClose);
		}
		private function onEnterFrameOpen(event:Event):void {
			if (alpha < 1) {
				alpha += 0.1;
			} else {
				removeEventListener(Event.ENTER_FRAME, onEnterFrameOpen);
			}
		}
		private function onEnterFrameClose(event:Event):void {
			if (alpha > 0) {
				alpha -= 0.1;
			} else {
				removeEventListener(Event.ENTER_FRAME, onEnterFrameClose);
				if (parent != null) {
					parent.removeChild(this);
				}
			}
		}
		public function adaptSizeToStage(s:Stage):void {
			/*
			 * Adatta la dimensione "normale" della foto in base al
			 * lato piu' corto dello stage.
			 * Es.: se lo stage e' 640x480, la foto sara' avra al massimo
			 * altezza e larghezza di 480*0.8 = 384;
			 */
			if (s.stageWidth > s.stageHeight) {
				maxSize_ = s.stageHeight * 0.8;
			} else {
				maxSize_ = s.stageWidth * 0.8;
			}
		}
		private function drawBg():void {
			trace(maxSize_);
			graphics.beginFill(0x000000, 0.1);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
		}
		private function center():void {
			img_.x = (stage.stageWidth - img_.width) / 2;
			img_.y = (stage.stageHeight - img_.height) / 2;
		}
		private function rescale():void {
			/* riscala per stare dentro allo stage, ma mantiene proporzioni */
			const k:Number = 0.8;
			var w:Number = img_.width;
			var h:Number = img_.height;
			/* se troppo largo */
			if (w > stage.stageWidth * k) { 
				trace("too large");
				w = stage.stageWidth * k;
				h *= w / img_.width;
			}
			/* se troppo alto */
			if (h > stage.stageHeight * k) {
				trace("too high");
				h = stage.stageHeight * k;
				w *= h / img_.height;
			}
			img_.width = w;
			img_.height = h;
		}
		private function addProgressBar():void {
			pb_ = new ProgressBar();
			pb_.mode = ProgressBarMode.POLLED;
			pb_.x = (stage.stageWidth - pb_.width) / 2;
			pb_.y = (stage.stageHeight - pb_.height) / 2;
			pb_.source = img_.contentLoaderInfo;
		}
	}
}